﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcLibrary.Repository;
using System.IO;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminImageDestinationController : Controller
    {
        //
        // GET: /AdminCP/AdminImageDestination/

        public ActionResult Index(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = from p in db.EntityImageDestinations
                            where p.IdDestination == id
                            select p;
                ViewBag.IdDestination = id;
                ViewBag.DestinationName = db.EntityDestinations.FirstOrDefault(p => p.Id==id).Name;
                return View(query.ToList());
            }
        }

        public ActionResult Edit(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = db.EntityImageDestinations.FirstOrDefault(p => p.Id == id);
                ViewBag.IdDestination = query.IdDestination;
                return View(query);
            }
        }

        [HttpPost]
        public ActionResult Edit(EntityImageDestination model, HttpPostedFileBase Image)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityImageDestination ImageDestination = db.EntityImageDestinations.FirstOrDefault(p => p.Id == model.Id);
                if(ImageDestination!=null)
                {
                ImageDestination.IdDestination = model.IdDestination;
                ImageDestination.Position = model.Position;
                if (Image != null && Image.ContentLength > 0)
                    {
                        int kb = Image.ContentLength / 1024; //file size kb
                        if (kb >= 2048) // file qua lon
                        {
                            return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                        }
                        //delete file cu
                        if (ImageDestination.Image != null)
                        {
                            string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/destination/"), ImageDestination.Image); // file hinh cu
                            System.IO.File.Delete(fileToDelete);
                        }

                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(Image);

                        if (encodestring == "!")
                            return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                        var path = Path.Combine(Server.MapPath("~/Content/upload/destination/"), encodestring);
                        Image.SaveAs(path);

                        ImageDestination.Image = encodestring;
                    }
                    db.SubmitChanges();
                    return RedirectToAction("Index", new { id = model.IdDestination });
                }
                    else
                    return View("Error");
            }
        }

        public ActionResult Insert(int id)
        {
            ViewBag.IdDestination = id;
            return View();
        }

        [HttpPost]
        public ActionResult Insert(EntityImageDestination model, int IdDestination,HttpPostedFileBase Image)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext()) 
            {
                EntityImageDestination ImageDestination = new EntityImageDestination();
                if (ImageDestination != null)
                {
                    ImageDestination.IdDestination = IdDestination;
                    ImageDestination.Position = model.Position;
                    if (Image != null && Image.ContentLength > 0)
                    {
                        int kb = Image.ContentLength / 1024; //file size kb
                        if (kb >= 2048) // file qua lon
                        {
                            return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                        }
                        //delete file cu
                        if (ImageDestination.Image != null)
                        {
                            string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/destination/"), ImageDestination.Image); // file hinh cu
                            System.IO.File.Delete(fileToDelete);
                        }

                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(Image);

                        if (encodestring == "!")
                            return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                        var path = Path.Combine(Server.MapPath("~/Content/upload/destination/"), encodestring);
                        Image.SaveAs(path);

                        ImageDestination.Image = encodestring;
                    }
                    db.EntityImageDestinations.InsertOnSubmit(ImageDestination);
                    db.SubmitChanges();
                    return RedirectToAction("Index", new { id = model.IdDestination });
                }
                else
                    return View("Error");
            }
        }

        public string Delete(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
               
                EntityImageDestination ImageDestination = db.EntityImageDestinations.FirstOrDefault(p => p.Id == id);
                if (ImageDestination != null)
                {
                    if (ImageDestination.Image != null)
                    {
                        string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/destination/"), ImageDestination.Image); // file hinh cu
                        System.IO.File.Delete(fileToDelete);
                    }
                    db.EntityImageDestinations.DeleteOnSubmit(ImageDestination);
                    db.SubmitChanges();
                    return "Ok";
                }
                else
                    return "Error";
            }
        }
    }
}
